package com.frinder.base.utils;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;

import java.security.MessageDigest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName StringUtils
 * @Author frinder
 * @Description 字符串辅助类
 * @Date 2019/1/21 11:12
 * @Version 1.0
 */
public class StringUtils {

    public final static DateTimeFormatter YEAR_MONTH_LINE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");

    public final static DateTimeFormatter FULL_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    public static final String SPACE_REGEX = "\\s*";

    public static void main(String[] args) {
        System.out.println(get69Code());
    }

    /**
     * @Author frinder
     * @Description 使用 # 格式化数组
     * @Date 2019/3/22 14:39
     * @Param [args]
     * @Return java.lang.String
     */
    public static String formatByWell(String... args) {
        return format("#", args);
    }

    public static String format(String split, String... args) {
        return Joiner.on(split).join(args);
    }

    public static String format(String split, List<String> args) {
        return Joiner.on(split).join(args);
    }

    /**
     * @Author frinder
     * @Description 获取13位69码
     * @Date 2019/2/16 10:07
     * @Param []
     * @Return java.lang.String
     */
    public static String get69Code() {
        int first = 69;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {
            //有可能是负数
            hashCodeV *= -1;
        }
        // 0 代表前面补充0
        // 11 代表长度为11
        // d 代表参数为正数型
        return first + String.format("%011d", hashCodeV);
    }

    /**
     * @Author frinder
     * @Description 获取标识
     * @Date 2019/2/1 14:54
     * @Param [args]
     * @Return java.lang.String
     */
    public static String getSid(String... args) {
        return encrypt32(formatByWell(args));
    }

    /**
     * 将 encryptStr 转化为 32位字符串
     *
     * @param encryptStr
     * @return
     */
    public static String encrypt32(String encryptStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            encryptStr = hexValue.toString();
        } catch (Exception e) {
        }
        return encryptStr;
    }

    /**
     * @Author frinder
     * @Description 去除字符串中所有空格
     * @Date 2018/11/29 11:41
     * @Param [resource]
     * @Return java.lang.String
     */
    public static String trimAll(String resource) {
        return resource.replaceAll(SPACE_REGEX, "");
    }

    /**
     * @Author frinder
     * @Description 获取 traceId
     * @Date 2019/1/21 11:51
     * @Param []
     * @Return java.lang.String
     */
    public static String getTraceId() {
        return getSid("T");
    }

    /**
     * @Author frinder
     * @Description 获取一个特定格式标识
     * @Date 2019/1/26 14:53
     * @Param [prefix]
     * @Return java.lang.String
     */
    public static String getSid(String prefix) {
        return prefix + LocalDateTime.now().format(FULL_DATE_FORMATTER);
    }

    /**
     * 返回 “YYYY-MM” 格式字符串
     * step: 0，返回当前月
     * step: -1，返回上月
     * step: 1，返回下月
     *
     * @param step
     * @return
     */
    public static String getMonthStr(int step) {
        return LocalDate.now().plusMonths(step).format(YEAR_MONTH_LINE_FORMATTER);
    }

    /**
     * 下划线转大写
     *
     * @param resource
     * @return
     */
    public static String underline2Upper(String resource) {
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, resource);
    }

    /**
     * 大写转下划线
     *
     * @param resource
     * @return
     */
    public static String upper2Underline(String resource) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, resource);
    }

    /**
     * 首字母转大写
     *
     * @param resource
     * @return
     */
    public static String first2Upper(String resource) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, resource);
    }

    /**
     * 首字母转小写
     *
     * @param resource
     * @return
     */
    public static String first2Lower(String resource) {
        return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, resource);
    }

    /**
     * @Author frinder
     * @Description 获取正则截取第一个字符串
     * @Date 2019/4/4 12:53
     * @Param [pattern, sourceStr]
     * @Return java.lang.String
     */
    public static String getStartRegexStr(Pattern pattern, String sourceStr) {
        Matcher matcher = pattern.matcher(sourceStr);
        if (matcher.find()) {
            return matcher.group();
        }
        return "";
    }

}
